﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace org.xisoft.game.utils.game.algorithms
{
    public abstract class AiAlgorithm<Broad>
         where Broad: ChessBoardBase
    {
        public int MaxDeep { get; protected set; } = 3;
        public abstract List<MoveAction> GetAllCanMoves(Broad broad);

        public MoveAction Search(Broad broad)
        {
            return Search(broad, MaxDeep);
        }

        public virtual void DoNext(Broad broad,MoveAction moveAction)
        {
            broad.DoNextMove(moveAction);
        }

        public abstract MoveAction Search(Broad broad, int depth);

        public virtual bool CanMove(Broad board, int x, int y)
        {
            if (board[x, y] != null)
                return false;
            return true;
        }


        public event EventHandler ThinkingEvent;


        protected void ResumeThinking()
        {
            ThinkingEvent?.Invoke(this, EventArgs.Empty);
        }

        public virtual bool? GetWinColor(Broad chessBoard)
        {
            return null;
        }

        public virtual  List<MoveAction> GetSortedAllCanMoves(Broad broad)
        {
            List<MoveAction> list = GetAllCanMoves(broad);

            list.Sort((a, b) => Math.Sign(b.Score - a.Score));

            return list;
        }

    }
}
